package de.doegel.common.widget;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import de.doegel.common.R;

public class KeyValueIntentListAdapter extends BaseAdapter {
	protected Context context;
	protected List<KeyValueIntentItem> all;

	public KeyValueIntentListAdapter(Context context, List<KeyValueIntentItem> all) {
		this.context = context;
		this.all = all;
	}

	public int getCount() {
		return all.size();
	}

	public KeyValueIntentItem getItem(int position) {
		return all.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	public boolean isEnabled(int position) {
		return getItem(position).intent != null;
	}
	
	public View getView(int position, View convertView, ViewGroup parent) {
		View res = convertView;
		if (res == null)
			res = LayoutInflater.from(context).inflate(R.layout.key_value_intent_item, null);

		TextView keyText = (TextView) res.findViewById(R.id.item_key);
		TextView valueText = (TextView) res.findViewById(R.id.item_value);
		ImageView disclosureImage = (ImageView) res.findViewById(R.id.item_disclosure);

		KeyValueIntentItem item = getItem(position);
		keyText.setText(item.key);
		valueText.setText(item.value);
		if (item.intent != null) {
			disclosureImage.setVisibility(View.VISIBLE);
		} else {
			disclosureImage.setVisibility(View.INVISIBLE);
		}

		return res;
	}
}
